import type {EChartOption} from "echarts";

type Format = EChartOption.Tooltip.Format;

export type TooltipUnitFunc = (item: Format) => string;

export type TooltipUnitMap = Map<string | number, string>;

export type TooltipUnitObj = {
    [attr in string | number]: string;
};

export type TooltipUnitOtherType = TooltipUnitFunc | TooltipUnitMap | TooltipUnitObj;

let isUseSeriesName = false;

export function updateIsUseSeriesName(isUseSeriesNameNew: boolean): void {
    isUseSeriesName = isUseSeriesNameNew;
}

function getUnitFrom(unit: string | string[] | TooltipUnitOtherType, item: Format): string {
    const seriesIndex = item.seriesIndex || 0;
    if (unit instanceof Array) {
        return unit[seriesIndex];
    }
    const seriesName = item.seriesName || "";
    if (unit instanceof Map) {
        if (unit.has(seriesName)) {
            return unit.get(seriesName) || "";
        }
        return unit.get(seriesIndex) || "";
    }
    if (typeof unit === "object") {
        if (
            seriesName &&
            Object.prototype.hasOwnProperty.call(unit, seriesName)
        ) {
            return unit[seriesName];
        }
        return unit[seriesIndex] || "";
    }
    if (typeof unit === "function") {
        return unit(item);
    }
    return unit;
}

function getSeriesNameOf(name: string | undefined): string {
    if (!name || (name.match(/^series\d+$/) && !isUseSeriesName)) {
        return "";
    }
    return name;
}

function getTextOfNum(val: string | number): string {
    if (isNaN(parseFloat(val as string))) {
        return "-";
    }
    return val as string;
}

function getTooltipItemHTML(item: Format, unit: string) {
    return `
<div class="item-content">
    <span class="tooltip-item-marker">${item.marker}</span>
    <span class="tooltip-item-series-name">${getSeriesNameOf(item.seriesName)}</span>
    <span class="tooltip-item-value-unit">
        <span class="tooltip-item-value">${getTextOfNum(item.value as any as string)}</span>
        <span class="tooltip-item-unit">${unit}</span>
    </span>
</div>
`;
}

export function tooltipAddUnitOfItemList(itemList: Format[], unit: string | string[] | TooltipUnitOtherType = "", nameUnit: string = ""): string {
    let html = itemList.map((item, index) => {
        const curUnit = getUnitFrom(unit, item);
        return getTooltipItemHTML(item, curUnit);
    }).join("");
    return `
<div class="custom-echarts-tooltip tooltip-item-list">
    <div class="series-name">${itemList[0].name}${unit}</div>
    <div class="item-list-content">${html}</div>
</div>
    `;
}

export function tooltipAddUnitOfSingle(item: Format, unit: string = "", nameUnit: string = ""): string {
    return `
        <div class="custom-echarts-tooltip tooltip-single">
            <div class="name">${item.name}${nameUnit}</div>
            ${getTooltipItemHTML(item, unit)}
        </div>
    `;
}
export function tooltipAddUnit(item: Format, unit?: string, nameUnit?: string): string
export function tooltipAddUnit(item: Format[], unit?: string | string[] | TooltipUnitOtherType, nameUnit?: string): string
export function tooltipAddUnit(item: Format | Format[], unit: string | string[] | TooltipUnitOtherType = "", nameUnit: string = ""): string {
    if (item instanceof Array) {
        return tooltipAddUnitOfItemList(item, unit, nameUnit);
    }
    return tooltipAddUnitOfSingle(item, unit as string, nameUnit);
}
